﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gqq.Csharpindepth.Designpatter
{
    public class Singleton
    {
        /// <summary>
        /// 私有了构造函数
        /// </summary>
        private Singleton()
        {
        }

        private static Singleton _instance;
        
        /// <summary>
        /// 创建对象的方法
        /// </summary>
        /// <returns></returns>
        public static Singleton CreateInstance()
        {
            return _instance ?? (_instance = new Singleton());
        }

        public int Value { get; set; }
    }

    /// <summary>
    /// 第二类单件模式（最简单的单件模式）
    /// </summary>
    public class Singleton2
    {
        private Singleton2()
        {
        }

        public static readonly Singleton2 Instance = new Singleton2();

        public int Value { get; set; }
    }

    /// <summary>
    /// 第三个单件模式，其实是一样的。
    /// </summary>
    public class Singleton3
    {
        private Singleton3()
        {
        }

        public int Value { get; set; }
        public static readonly Singleton3 Instance;

        /// <summary>
        /// 在静态构造函数中初始化Singleton3
        ///
        /// </summary>
        static Singleton3()
        {
            Instance = new Singleton3();
        }
    }
}
